package com.yanqu.road.server.timer.activity;

import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.system.CommonMgr;
import com.yanqu.road.utils.LocalDateTimeHelper;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Calendar;

public class ActivitySyncTask extends GeneralTask {

    private Logger logger = LogManager.getLogger(this.getClass().getName());

    private long lastSyncTime = 0;

    @Override
    public void doRun() {
        //每天最后10分钟
        Calendar lastTenMinute = Calendar.getInstance();
        lastTenMinute.setTimeInMillis(LocalDateTimeHelper.getZeroTimeTimeStamp() + DateHelper.DAY_MILLIONS - 10 * DateHelper.MINUTE_MILLIONS);
        long lastTenMinuteMillion = lastTenMinute.getTimeInMillis();
        long nowTime = System.currentTimeMillis();
        if(lastSyncTime == DateHelper.getTodayZeroTime()){
            return;
        }

        if(nowTime >= lastTenMinuteMillion) {
            for (GamePlayer player : GamePlayerMgr.getCopyPlayerMap().values()) {
                try {
                    player.getModule(ActivityModule.class).syncActivity();
                } catch (Exception ex) {
                    logger.error("ActivitySyncTask error", ex);
                }
            }
            try{
                //重新加载语言
                ServerLanguageMgr.reloadLanguage();
                //重新载入通用道具
                CommonMgr.reloadCommonData();
            }catch (Exception e){
                logger.error("ActivitySyncTask goods reload error:", e);
            }
            lastSyncTime = DateHelper.getTodayZeroTime();
        }
    }
}
